\documentclass[a4paper,10pt]{report}
\usepackage{mathpartir}

\newcommand{\syntaxterm}[2]{\item[$#1$] \hspace{\stretch{1}}\textbf{\emph{#2}}}
\newenvironment{syntaxdef}[2]
    {\begin{description}
        \item[$#1$] $::=$ 
        \hspace{\stretch{1}}
        \textbf{\emph{#2}}
    \begin{description}}
    {\end{description}\end{description}}


\begin{document}

\title{APython and APost}
\author{Johannes Woolard}
\date{}
\maketitle

\begin{abstract}
APython is a statically typed language with a syntax based on Python\footnote
{see http:\slash\slash www.python.org}. 
Among other things it supports first-class closures and currying.

APost\footnote{APost is actually similar in many respects to PostScript} is a very simple language with closures, records, 
lists and integer and boolean types.
It is little more than an elaborate virtual machine (and is used as such in my implementation of APython).

My project originally set out to produce a compiler for a relatively large subset of the Python language, but it has become a study in type inference in
a strongly typed, imperative, language with subtyping, first class functions and records. I have also put a lot of effort into transforming
an object oriented language (APython) into a simpler imperative one to make compilation easier.

\end{abstract}

\tableofcontents


\chapter{Introduction to APython}
APython\footnote{So called because in the Python world it is fashionable to refer to nice code (especially code that makes good use of dynamic-typing
and introspection)
as Pythonic. Most Python people would consider APython code Apythonic.} is a simple object-oriented language originally inspired by Guido van Rossum's
Python (henceforth referred to as CPython).

Like CPython, APython has syntactic whitespace. APython also uses many CPython keywords. However, APython does not support tuples, lists, dictionaries,
exceptions, continuations, introspection, dynamic code execution (ie. the eval() and exec() functions) and hundreds of other nifty features that make 
CPython so easy to use. This is not to say that APython is a simple language: it has an extremely expressive type system and the implementation that I
have produced is surprisingly fast (considerably faster than CPython).

While the full APython language supports objects, they are really just syntactic sugar for records.
\chapter{The Type System}

\chapter{Compiler Implementation}

\chapter{The APost Virtual Machine}

The APost virtual machine has an argument stack, a program input buffer and a reference to the active environment. Initially the stack is empty, the environment reference points to an empty environment (the global environment) and the input buffer contains the program to be executed.

The buffer is nothing more than a sequence of commands, which are executed in order.
The implementation translates the APost sourcecode into a sequence of command primitives before starts - this way the (relatively slow) process of lexing and parsing is only done once.

There are 3 types of command:
\begin{itemize}
\item Primitives
\item Built-in commands
\item Procedure calls
\end{itemize}

\section{Primitives}
Primitives always push exactly one item onto the stack. They are used to push constants into the machine. APost supports the following literals:
\begin{description}
\item[Integer] Any decimal integer
\item[String] Character sequences in double quotes
\item[Atom] Character sequences on their own
\end{description}

\section{Built-in Commands}
There are only a few built-in commands:
\begin{description}
\item[\slash load] Load a value from the environment. Takes an atom.
\item[\slash store] Store a value into the current top level environment. Takes an atom and a value.
\item[\slash get] Get a value from a record, takes a record and an atom.
\item[\slash set] Set a value in a record, takes a record, an atom and a value.
\item[\slash print] Print to stdout, takes a string.
\item[\slash call] Call a procedure (pushing a new environment), takes a procedure.
\item[\slash branch] Branches. Takes two procedures and a boolean.
\item[\slash loop] Takes two procedures: a guard and a body.
\end{description}

\appendix
\chapter{The APython Compiler Source Code}
The APython compiler is written in OCaml, using ocamllex and ocamlyacc for lexing and parsing.

\chapter{The APost Interpreter Source Code}
The APost Interpreter is written in OCaml, using ocamllex and ocamlyacc for lexing and parsing.

\begin{thebibliography}{1}
\bibitem[Pierce]{pierce02}``Types and Programming Languages'', Benjamin C. Pierce, MIT Press (2002)
\bibitem[Wright \& Felleisen]{wright92}``A Syntactic Approach to Type Soundness'', Andrew K. Wright \& Matthias Felleisen,
Information and Computation, 115(1):38--94, 1994.
\end{thebibliography}
\end{document}
